package com.campus.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.merchant.entity.Order;
import com.campus.merchant.mapper.OrderMapper;
import com.campus.merchant.vo.merchant.OverviewDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 商户首页经营数据概览Service
 * @author AI
 */
@Service
public class OverviewDataService {
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 获取商户首页经营数据概览
     * @param merchantId 商户ID
     * @return OverviewDataVo
     */
    public OverviewDataVo getOverviewData(Integer merchantId) {
        LocalDateTime startOfDay = LocalDateTime.now().toLocalDate().atStartOfDay();
        LocalDateTime now = LocalDateTime.now();
        // 今日订单数
        LambdaQueryWrapper<Order> todayOrderWrapper = new LambdaQueryWrapper<>();
        todayOrderWrapper.eq(Order::getMerchantId, merchantId)
                .ge(Order::getCreateTime, startOfDay)
                .le(Order::getCreateTime, now);
        long todayOrderCount = orderMapper.selectCount(todayOrderWrapper);
        // 今日营业额（已支付订单的实付金额）
        BigDecimal todayRevenue = orderMapper.getTodayRevenue(merchantId);
        // 待处理订单数（待接单）
        LambdaQueryWrapper<Order> pendingWrapper = new LambdaQueryWrapper<>();
        pendingWrapper.eq(Order::getMerchantId, merchantId)
                .eq(Order::getStatus, 2);
        long pendingOrderCount = orderMapper.selectCount(pendingWrapper);
        OverviewDataVo vo = new OverviewDataVo();
        vo.setTodayOrderCount(todayOrderCount);
        vo.setTodayRevenue(todayRevenue.doubleValue());
        vo.setPendingOrderCount(pendingOrderCount);
        return vo;
    }
}